package com.cloud.business.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.business.dao.sys.SysPermissionMapper;
import com.cloud.business.dao.sys.SysRolePermissionMapper;
import com.cloud.business.entity.sys.SysPermission;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.syspermissionvm.SysPermissionQueryModel;
import com.cloud.business.model.sys.syspermissionvm.SysPermissionVM;
import com.cloud.business.service.sys.ISysPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统操作权限表 服务实现类
 * </p>
 *
 * @author dh
 * @since 2020-03-21
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {

    private final SysPermissionMapper sysPermissionMapper;
    private final SysRolePermissionMapper sysRolePermissionMapper;

    @Autowired
    public SysPermissionServiceImpl(SysPermissionMapper sysPermissionMapper,
                                    SysRolePermissionMapper sysRolePermissionMapper) {
        this.sysPermissionMapper = sysPermissionMapper;
        this.sysRolePermissionMapper = sysRolePermissionMapper;
    }

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param sysPermissionQueryModel
     * @param page
     * @param msg
     * @throws Exception
     */
    @Override
    public void getListByPage(SysPermissionQueryModel sysPermissionQueryModel, IPage<SysPermissionVM> page, ResultMsg msg) throws Exception {

        msg.setMsg("获取成功");

        QueryWrapper<SysPermission> sysPermissionQueryWrapper = new QueryWrapper<>();
        sysPermissionQueryWrapper.lambda()
                .eq(!StringUtils.isEmpty(sysPermissionQueryModel.getMenuCode()), SysPermission::getMenuCode, sysPermissionQueryModel.getMenuCode())
                .and(!StringUtils.isEmpty(sysPermissionQueryModel.getQueryStr()),
                        qr -> qr.like(SysPermission::getName, sysPermissionQueryModel.getQueryStr())
                                .or()
                                .like(SysPermission::getPermission, sysPermissionQueryModel.getQueryStr())
                                .or()
                                .like(SysPermission::getService, sysPermissionQueryModel.getQueryStr())
                )
                ;

        if (page.getSize() == 0) {
            List<SysPermission> sysPermissionList = sysPermissionMapper.selectList(sysPermissionQueryWrapper);
            if (sysPermissionList.size() > 0) {
                page.setTotal(sysPermissionList.size());
                List<SysPermissionVM> sysPermissionVMList = new ArrayList<>();
                List<Integer> permissionIds = new ArrayList<>();
                if (sysPermissionQueryModel.getRoleId() != null && sysPermissionQueryModel.getRoleId() != 0) {
                    permissionIds = sysRolePermissionMapper.getPermissionIdsByRoleId(sysPermissionQueryModel.getRoleId());
                }
                List<Integer> finalPermissionIds = permissionIds;
                sysPermissionList.forEach(item -> {
                    SysPermissionVM sysPermissionVm = new SysPermissionVM();
                    BeanUtils.copyProperties(item, sysPermissionVm);
                    if (sysPermissionQueryModel.getRoleId() != null && sysPermissionQueryModel.getRoleId() != 0) {
                        sysPermissionVm.setCheckFlag(finalPermissionIds.contains(item.getObjectId()));
                    }
                    sysPermissionVMList.add(sysPermissionVm);
                });
                page.setRecords(sysPermissionVMList);
            }
            return;
        }

        IPage<SysPermission> iPage = new Page<>(page.getCurrent(), page.getSize());
        sysPermissionMapper.selectPage(iPage, sysPermissionQueryWrapper);
        page.setTotal(iPage.getTotal());

        if (iPage.getRecords().size() > 0) {
            List<SysPermissionVM> sysPermissionVMList = new ArrayList<>();
            List<Integer> permissionIds = new ArrayList<>();
            if (sysPermissionQueryModel.getRoleId() != null && sysPermissionQueryModel.getRoleId() != 0) {
                permissionIds = sysRolePermissionMapper.getPermissionIdsByRoleId(sysPermissionQueryModel.getRoleId());
            }
            List<SysPermission> sysPermissionList = iPage.getRecords();
            List<Integer> finalPermissionIds = permissionIds;
            sysPermissionList.forEach(item -> {
                SysPermissionVM sysPermissionVm = new SysPermissionVM();
                BeanUtils.copyProperties(item, sysPermissionVm);
                if (sysPermissionQueryModel.getRoleId() != null && sysPermissionQueryModel.getRoleId() != 0) {
                    sysPermissionVm.setCheckFlag(finalPermissionIds.contains(item.getObjectId()));
                }
                sysPermissionVMList.add(sysPermissionVm);
            });
            page.setRecords(sysPermissionVMList);
        }
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param sysPermission
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer create(SysPermission sysPermission, ResultMsg msg) throws Exception {
        Date now = new Date();
        sysPermission.setCreateTime(now);
        sysPermission.setModificationTime(now);
        int count = sysPermissionMapper.insert(sysPermission);
        msg.setMsg("创建成功");
        return sysPermission.getObjectId();
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param sysPermission
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer update(SysPermission sysPermission, ResultMsg msg) throws Exception {
        Date now = new Date();
        sysPermission.setModificationTime(now);
        int count = sysPermissionMapper.updateById(sysPermission);
        msg.setMsg("更新成功");
        return sysPermission.getObjectId();
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param operationId
     * @param objectId
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public SysPermission getById(Integer operationId, Integer objectId, ResultMsg msg) throws Exception {
        SysPermission sysPermission = sysPermissionMapper.selectById(objectId);
        msg.setMsg("获取成功");
        return sysPermission;
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByList(DeletePost deletePost, ResultMsg msg) throws Exception {
        msg.setMsg("删除成功");
        int count = sysPermissionMapper.deleteBatchIds(deletePost.getKeyValues());
        if (count <= 0) {
            throw new Exception("删除失败");
        }
        return count;
    }
    //endregion

}
